package four_RomanToInteger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class CSolution {

    public static void main(String[] args) {
        CSolution cSolution = new CSolution();
        System.out.println(cSolution.romanToInt("III"));
        System.out.println(cSolution.romanToInt("IV"));
        System.out.println(cSolution.romanToInt("IX"));
        System.out.println(cSolution.romanToInt("LVIII"));
        System.out.println(cSolution.romanToInt("MCMXCIV"));
    }

    private int romanToInt(String s) {
        int result = 0;
        if(s == null) {
            return result;
        }

        char[] characters = s.toCharArray();
        for(char myChar:characters) {
            result = result+getValue(myChar);
        }
        if(s.contains("IX") || s.contains("IV")) {
            result = result - 2;
        }
        if(s.contains("XL") || s.contains("XC")) {
            result = result - 20;
        }
        if(s.contains("CD") || s.contains("CM")) {
            result = result - 200;
        }
        return result;
    }

    private int getValue(char myChar) {
        switch (myChar) {
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
                default:
                    return 0;
        }
    }
}
